package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.RetrieveByEmailV3Bean;
import com.yiwei.zhh.util.ImageUtil;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.FlowViewHorizontal;
import com.yiwei.zhh.view.V19FrameLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinFindByEmailActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_tips1)
    TextView txtTips1;
    @BindView(R.id.txt_tips2)
    TextView txtTips2;
    @BindView(R.id.edit_email)
    EditText editEmail;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_idno)
    EditText editIdno;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.step_view)
    FlowViewHorizontal stepView;
    private Unbinder unbinder;
    private String ctoken;
    private String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_find_way_email_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        String[] stepTitles = new String[]{"选择方式", "验证账户", "重置密码"};
        stepView.setProgress(2, 3, stepTitles, null);
        email = getIntent().getStringExtra("email");
        editEmail.setText(email);
        ctoken = getIntent().getStringExtra("ctoken");
    }

    @OnTextChanged({R.id.edit_name, R.id.edit_idno})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editIdno)) && !TextUtils.isEmpty(UI.toString(editName))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getRetrieveByEmailV3(String ctoken, String realname, String idno) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        RetrieveByEmailV3Bean bean = new RetrieveByEmailV3Bean();
        bean.ctoken = ctoken;
        bean.realname = realname;
        bean.idno = idno;
        Api.api_service.getRetrieveByEmailV3(bean).compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    if (model.getStatus() == 1) {
                        Intent intentPhone = new Intent(this, V3XueXinFindByEmailResetActivity.class);
                        intentPhone.putExtra("email", email);
                        startActivity(intentPhone);
                        finish();
                        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    } else {
                        Dialog.showResetFail(this);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    try {
                        getRetrieveByEmailV3(ctoken, URLEncoder.encode(editName.getText().toString(), "UTF-8"), UI.toString(editIdno));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editIdno))) {
            ToastUtil.makeText(getApplicationContext(), "身份证不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editName))) {
            ToastUtil.makeText(getApplicationContext(), "姓名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
